<?php
	/**
	 * 格式化打印数组
	 */
	function p ($arr){
		echo '<pre>';
		print_r($arr);
		echo '</pre>';
	}

	/**
	 * 递归无限级分类
	 * @param  [type]  $arr   要处理的数组
	 * @param  integer $pid   父类id
	 * @param  integer $type  处理类型
	 * @param  integer $level 所属级别
	 * @return [array]        处理好的数组
	 */
	function recursion($arr , $pid=0 , $type=1 , $level=0){

		switch($type){
			case 1: //递归返回所有无限级栏目
				static $array1 = array();
				static $id1 = 0;
				foreach($arr as $val){
					if($val['pid'] == $pid){
						$array1[$id1] = $val;
						$array1[$id1]['html'] = str_repeat('--', $level);
						$id1++;
						recursion($arr , $val['id'] , $type ,$level+1);
					}
				}
				return $array1;
				break;
			case 2: //传递分类id返回所有父级
				static $array2 = array();				
				foreach($arr as $val){
					if($val['id'] == $pid){
						$array2[$val['id']] = $val;
						recursion($arr , $val['pid'] , $type);
					}
				}
				return array_reverse($array2);
				break;
			case 3: //递归返回所有无限级栏目,并标记顶级分类
				static $array3 = array();
				static $id3 = 0;
				foreach($arr as $val){
					if($val['pid'] == $pid){
						if($pid == 0){
							$tmpVal = $val['name'];
							$val['name'] = '<font size="2"><b>'.$tmpVal.'</b></font>';
						}
						$array3[$id3] = $val;
						$array3[$id3]['html'] = str_repeat('--', $level);
						$id3++;
						recursion($arr , $val['id'] , $type ,$level+1);
					}
				}
				return $array3;
				break;
			case 4://传递一个id返回所有子类的id
				static $array4 = array();
				static $id4 = 0;
				foreach($arr as $val){
					if($val['pid'] == $pid){
						$array4[$id4] = $val;
						$array4[$id4]['html'] = str_repeat('--', $level);
						$id4++;
						recursion($arr , $val['id'] , $type , $level+1);
					}
				}
				return $array4;
				break;
			case 5: //按父子关系等级返回多维数组
				$array = array();
				foreach($arr as $val){
					if($val['pid'] == $pid){
						$array[$val['id']] =$val;
						$array[$val['id']]['children'] = recursion($arr , $val['id'] , $type);
					}
				}
				return $array;
				break;
			default :
				return false;
		}
		
	}
?>